package com.yootk.provider.action;

import com.yootk.common.dto.MemberDTO;
import com.yootk.jwt.service.IEncryptService;
import com.yootk.jwt.service.ITokenService;
import com.yootk.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/token/*")
public class TokenAction {
    @Autowired
    private IMemberService memberService; // 本模块提供的
    @Autowired
    private IEncryptService encryptService; // yootk-starter-jwt模块提供的
    @Autowired
    private ITokenService tokenService; // yootk-starter-jwt模块提供的
    @RequestMapping("create")
    public Object login(@RequestBody MemberDTO memberDTO) {
        // 对用户传入的密码信息进行加密处理
        memberDTO.setPassword(this.encryptService.getEncryptPassword(memberDTO.getPassword()));
        Map<String, Object> result = this.memberService.login(memberDTO); // 登录业务处理
        if (((Boolean)result.get("status"))) {  // 登录状态
            return this.tokenService.createToken(result.get("mid").toString(), (Map<String, Object>) result.get("resource"));
        }
        return null;
    }
    @RequestMapping("parse")
    public Object parseToken(String token) {
        return this.tokenService.parseToken(token); // Token解析处理
    }
}
